﻿using System.Windows.Data;
using System;

namespace TheVerge.Phone.Converters {
	public class FromNowDateTimeConverter : IValueConverter {

		#region IValueConverter Members

		public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (!(value is DateTime)) {
				return value;
			}

			var diff = DateTime.Now.Subtract((DateTime)value);

			if (diff.TotalMinutes < 45)
				return string.Format(@"{0} mins ago", diff.Minutes);
			else if (diff.TotalHours < 1.5)
				return @"an hr ago";
			else if (diff.TotalHours < 24)
				return string.Format("{0} hrs ago", diff.Hours);
			else if (diff.TotalDays < 2)
				return @"1 day ago";
			else if (diff.TotalDays < 7)
				return string.Format(@"{0} days ago", diff.Days);
			else if (diff.TotalDays < 10.5)
				return @"a week ago";
			else if (diff.TotalDays < 17.5)
				return @"two weeks ago";
			else if (diff.TotalDays < 24.5)
				return @"three weeks ago";
			else if (diff.TotalDays < 45)
				return @"a month ago";
			else if (diff.TotalDays < 365)
				return string.Format("{0} months ago", (int)Math.Round(diff.TotalDays / 30));
			else
				return "more than a year ago";
		}

		public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			throw new System.NotImplementedException();
		}

		#endregion
	}
}
